package com.service.lx;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.luo.CusDao;
import com.dao.lx.RepairorderDAO;
import com.dao.lx.RepairorderDAOMybatis;
import com.dao.zy.ProductDao;
import com.entity.Customer;
import com.entity.Product;
import com.entity.Repairorder;
import com.service.ServiceException;
import com.entity.ChartVo;
import com.entity.ChartVo2;
import com.utils.Pager;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepairorderService {
	@Autowired
	private RepairorderDAO repairorderDao;
	@Autowired
	private RepairorderDAOMybatis repairorderDaoM;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private CusDao cusDao;
	/**
	 * 添加/修改/逻辑删除
	 * @param complain
	 * @throws ServiceException
	 */
	public void addAndUpdateRepairorder(Repairorder repairorder) throws ServiceException{
		try {
			//判断产品是否存在
			if(repairorder.getProductId()!=null){
				Product product=productDao.findOne(repairorder.getProductId());
				if(product!=null){
					repairorder.setProduct(product);
				}else{
					repairorder.setProduct(null);
				}
			}
			//判断客户是否存在
			if(repairorder.getCusno()!=null){
				Customer customer=cusDao.findOne(repairorder.getCusno());
				if(customer!=null){
					repairorder.setCustomer(customer);
				}else{
					repairorder.setCustomer(null);
				}
			}
			Repairorder repairorder2=repairorderDao.findOne(repairorder.getRepairorderId());
			if(repairorder2==null){
				repairorder.setRepairorderIfdelete(new BigDecimal(1));
			}
			repairorder.setConfirm(null);
			repairorder.setDetection(null);
			repairorder.setProcess(null);
			repairorder.setDELIVERRECORD_copy(null);
			repairorder.setRepairorderIntodate(new Date());
			repairorderDao.save(repairorder);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param repairorderId
	 * @throws ServiceException
	 */
	public void deleteRepairorder(Long repairorderId) throws ServiceException{
		try {
			repairorderDao.delete(repairorderId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 逻辑删除
	 * @param repairorderId
	 */
	public void UpdateRepairorderIfdelete(Long repairorderId) {
		Repairorder repairorder = repairorderDao.findOne(repairorderId);
		repairorder.setRepairorderIfdelete(new BigDecimal(0));
		addAndUpdateRepairorder(repairorder);
	}
	
	/**
	 * 批量删除
	 * @param repairorderIds
	 */
	public void deletArrIfDelete(List<Long> repairorderIds) {
		if(repairorderIds != null) {
			for(Long repairorderId: repairorderIds) {
				Repairorder repairorder = repairorderDao.findOne(repairorderId);
				repairorder.setRepairorderIfdelete(new BigDecimal(0));
				addAndUpdateRepairorder(repairorder);
			}
		}
	}
	
	/**
	 * 单条查询
	 * @param repairorderId
	 * @return
	 */
	public Repairorder findOne(Long repairorderId) {
		return repairorderDao.findOne(repairorderId);
	}

	/**
	 * 快速查询：接件状况
	 * @param repairorderReceivestate
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Repairorder> findRepairorderReceivestate(String repairorderReceivestate, Integer curpage, Integer pagesize) {
		return repairorderDao.queryByRepairorderReceivestate(repairorderReceivestate, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：维修工单进度
	 * @param repairorderSchedule
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Repairorder> findRepairorderSchedule(String repairorderSchedule, Integer curpage, Integer pagesize) {
		return repairorderDao.queryByRepairorderSchedule(repairorderSchedule, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：主题/工单流水号
	 * @param repairorderNumber
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Repairorder> findRepairorderNumber(String repairorderNumber, Integer curpage, Integer pagesize) {
		return repairorderDao.queryByRepairorderNumber(repairorderNumber, new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 查询所有
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Repairorder> findAllRepairorders(Integer curpage, Integer pagesize){
		return repairorderDao.findAllRepairorderIfdelete1(new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询（全部数据，接件状况，维修工单进度，主题/工单流水号）
	 * @param field
	 * @param pagesize
	 * @return
	 */
	public Page<Repairorder> findFastAllRepairorders(String field, Integer curpage, Integer pagesize){
		String str;
		if(field.startsWith("repairorderNumber")){
			str=field.replace("repairorderNumber", "");
			return findRepairorderNumber(str, curpage, pagesize);
		}else if(field.startsWith("repairorderSchedule")){
			str=field.replace("repairorderSchedule", "");
			return findRepairorderSchedule(str, curpage, pagesize);
		}else if(field.startsWith("repairorderReceivestate")){
			str=field.replace("repairorderReceivestate", "");
			return findRepairorderReceivestate(str, curpage, pagesize);
		}else{
			return findAllRepairorders(curpage, pagesize);
		}
	}
	
	/**
	 * 高级查询
	 * @param qa
	 * @return
	 */
	public Pager<Repairorder> findHighAllRepairorders(Map map){
		Repairorder repairorder = (Repairorder) JSONObject.parseObject(map.get("list").toString(), Repairorder.class);
		Integer curpage=(Integer)map.get("curpage");
		Integer pagesize=(Integer)map.get("pagesize");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(repairorder.getRepairorderDateStartAndStop()!=null){
    		try {
    			repairorder.setRepairorderReceivedateStart(sdf.parse((String) repairorder.getRepairorderDateStartAndStop().get(0)));
    			repairorder.setRepairorderReceivedateStop(sdf.parse((String) repairorder.getRepairorderDateStartAndStop().get(1)));
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
        }
		List<Repairorder> list=(List<Repairorder>) repairorderDaoM.findTrim(repairorder);
		Pager<Repairorder> pager=new Pager<Repairorder>();
		if(list.size()!=0){
			list.get(list.size()-1);
			Integer start=(curpage-1)*pagesize;
			Integer stop=curpage*pagesize;
			if(list.size()<stop){
				stop=list.size();
			}
			if(list.size()<start){
				start=stop-list.size()%pagesize;
			}
			pager.setList(list.subList(start, stop));
			pager.setTotal(list.size());
		}else{
			pager.setList(new ArrayList());
			pager.setTotal(0);
		}
		return pager;
	}

	/**
	 * 选择维修方式
	 * @param repairorderId
	 * @param repairorderType
	 */
	public void updataRepairorderType(Long repairorderId, BigDecimal repairorderType) {
		Repairorder repairorder=repairorderDao.findOne(repairorderId);
		System.out.println(repairorder.getConfirm());
		System.out.println(repairorder.getProcess());
		System.out.println(repairorder.getDetection());
		if(repairorder!=null){
			repairorder.setRepairorderType(repairorderType);
		}
		addAndUpdateRepairorder(repairorder);
	}

	/**
	 * 修改维修进度
	 * @param repairorderId
	 * @param repairorderSchedule
	 */
	public void updataRepairorderSchedule(Long repairorderId, String repairorderSchedule) {
		Repairorder repairorder=repairorderDao.findOne(repairorderId);
		System.out.println(repairorder.getConfirm());
		System.out.println(repairorder.getProcess());
		System.out.println(repairorder.getDetection());
		if(repairorder!=null){
			repairorder.setRepairorderSchedule(repairorderSchedule);
		}
		addAndUpdateRepairorder(repairorder);
	}

	/**
	 * 面饼统计图
	 * @return
	 */
	public List<ChartVo> selectchartvo(){
		return repairorderDaoM.selectchartvo();
	}
	

	/**
	 * 柱状折线统计图
	 * @return
	 */
	public List<ChartVo2> selectchartvo2(){
		return repairorderDaoM.selectchartvo2();
	}
}
